2020-2021 Sunseeker Telemetry and Lighting System
tec_ex1_syncTimers.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430F51x2 Demo - TimerD0/1-TEC, Synchronizing timers, Normal timer mode
34 //
35 // Description: This code example shows how to configure the TEC module for
36 // synchronizing two timer instances (TD0 and TD1). In this example code, TD0 is
37 // the master timer and TD1 is the slave instance. The clock configuration of
38 // the master is applied to the slave. The counter length, high res enable
39 // and timer mode settings of the master are applied to the slave as well.
40 //
41 // Note: TDxCCR0 registers of both master and slave timer instance (TD0 and
42 // TD1 in this case) should be the same value
43 //
44 // ACLK = REFO = 32kHz; SMCLK = MCLK = DCO Clock = 12MHz; TD0 = TD1 = 10kHz
45 //
46 // MSP430F51x2
47 // -----------------
48 // /|\| XIN|-
49 // | | |
50 // --|RST XOUT|-
51 // | |
52 // | P1.6/TD0.0|--> CCR0 - 50% dutycycle
53 // | P2.1/TD1.0|--> CCR0 - 50% dutycycle
54 // | |
55 // | P1.7/TD0.1|--> CCR1 - 20% dutycycle
56 // | P2.2/TD1.1|--> CCR1 - 60% dutycycle
57 // | |
58 // | P2.0/TD0.2|--> CCR2 - 40% dutycycle
59 // | P2.3/TD1.2|--> CCR2 - 80S% dutycycle
60 //
61 //******************************************************************************
62 #include "driverlib.h"
63 
64 //*****************************************************************************
65 //
66 //Target frequency for MCLK in kHz
67 //
68 //*****************************************************************************
69 #define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ 24000
70 
71 //*****************************************************************************
72 //
73 //MCLK/FLLRef Ratio
74 //
75 //*****************************************************************************
76 #define UCS_MCLK_FLLREF_RATIO 374
77 
78 void main(void)
79 {
80 
81  //Stop WDT
82  WDT_A_hold(WDT_A_BASE);
83 
84  // Increase Vcore setting to level1 to support fsystem=12MHz
85  // NOTE: Change core voltage one level at a time..
86  PMM_setVCore(PMMCOREV_1);
87 
88  // Set DCO FLL reference = REFO
89  UCS_initClockSignal(UCS_FLLREF,
90  UCS_REFOCLK_SELECT,
91  UCS_CLOCK_DIVIDER_1
92  );
93 
94  // Set ACLK = REFO
95  UCS_initClockSignal(UCS_ACLK,
96  UCS_REFOCLK_SELECT,
97  UCS_CLOCK_DIVIDER_1
98  );
99 
100  // (N + 1) * FLLRef = Fdco
101  // (374 + 1) * 32768 = 12MHz
102  // Set FLL Div = fDCOCLK/2
103  // Enable the FLL control loop
104  UCS_initFLLSettle(UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ,
106  );
107 
108  // P1.6/TD0.0, P1.7,TD0.1, options select
109  // Output direction
110  GPIO_setAsPeripheralModuleFunctionOutputPin(
111  GPIO_PORT_P1,
112  GPIO_PIN6 | GPIO_PIN7
113  );
114 
115  // P2.0/TD0.2, P2.1/TD1.0, P2.2/TD1.1, P2.3/TD1.2, options select
116  // Output direction
117  GPIO_setAsPeripheralModuleFunctionOutputPin(
118  GPIO_PORT_P2,
119  GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3
120  );
121 
122 
123  // Configure Master Timer Instance - TimerD0
124  // Freq = 100kHz, TD0.1/TD0.2 PWM Period = 10us
125  Timer_D_initUpModeParam initUpParam = {0};
126  initUpParam.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
127  initUpParam.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
128  initUpParam.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
129  initUpParam.timerPeriod = 120;
130  initUpParam.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
131  initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
132  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
133  initUpParam.timerClear = TIMER_D_DO_CLEAR;
134  Timer_D_initUpMode(TIMER_D0_BASE, &initUpParam);
135 
136  Timer_D_initCompareModeParam initComp0Param = {0};
137  initComp0Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_0;
138  initComp0Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
139  initComp0Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE;
140  initComp0Param.compareValue = 120;
141  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp0Param);
142 
143  Timer_D_initCompareModeParam initComp1Param = {0};
144  initComp1Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_1;
145  initComp1Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
146  initComp1Param.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
147  initComp1Param.compareValue = 24;
148  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp1Param);
149 
150  Timer_D_initCompareModeParam initComp2Param = {0};
151  initComp2Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_2;
152  initComp2Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
153  initComp2Param.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
154  initComp2Param.compareValue = 48;
155  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp2Param);
156 
157  // Configure Slave Timer Instance - TimerD1 PWM outputs
158  Timer_D_initUpModeParam initUpParam1 = {0};
159  initUpParam1.clockSource = TIMER_D_CLOCKSOURCE_EXTERNAL_TDCLK;
160  initUpParam1.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
161  initUpParam1.clockingMode = TIMER_D_CLOCKINGMODE_AUXILIARY_CLK;
162  initUpParam1.timerPeriod = 120;
163  initUpParam1.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
164  initUpParam1.captureCompareInterruptEnable_CCR0_CCIE =
165  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
166  initUpParam1.timerClear = TIMER_D_DO_CLEAR;
167  Timer_D_initUpMode(TIMER_D1_BASE, &initUpParam1);
168 
169  Timer_D_initCompareModeParam initComp0Param1 = {0};
170  initComp0Param1.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_0;
171  initComp0Param1.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
172  initComp0Param1.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE;
173  initComp0Param1.compareValue = 120;
174  Timer_D_initCompareMode(TIMER_D1_BASE, &initComp0Param1);
175 
176  Timer_D_initCompareModeParam initComp1Param1 = {0};
177  initComp1Param1.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_1;
178  initComp1Param1.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
179  initComp1Param1.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
180  initComp1Param1.compareValue = 72;
181  Timer_D_initCompareMode(TIMER_D1_BASE, &initComp1Param1);
182 
183  Timer_D_initCompareModeParam initComp2Param1 = {0};
184  initComp2Param1.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_2;
185  initComp2Param1.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
186  initComp2Param1.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
187  initComp2Param1.compareValue = 96;
188  Timer_D_initCompareMode(TIMER_D1_BASE, &initComp2Param1);
189 
190  // Syncronizing master (TD0) and slave (TD1) timer instances
191  // Enable synchronized clear by enabling Aux clear of slave timer
192  TEC_enableAuxiliaryClearSignal(TEC1_BASE);
193 
194  // Clear timer counter, Up mode, Start timers
195  Timer_D_startCounter(TIMER_D0_BASE,
196  TIMER_D_UP_MODE);
197 
198  __bis_SR_register(LPM0_bits); // Enter LPM0
199  __no_operation(); // For debugger
200 
201 }
__no_operation()
#define UCS_MCLK_FLLREF_RATIO
void main(void)
#define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ